import os
from datetime import time

import yaml
from appium.webdriver.common.mobileby import MobileBy
from appium.webdriver.webdriver import WebDriver
from selenium.common.exceptions import NoSuchElementException

from ui_framework_demo.common.getfiledir import SCREENSHOTDIR
from ui_framework_demo.page.decorator import decorator


class BasePage:
    """
    将公共的部分放入base类中，在别的类中直接继承就可以了
    将公共部分封装成方法，再在别的类中引用即可
    """
    def __init__(self,driver:WebDriver):
        self.driver = driver

    @decorator
    def find(self,locator,value):
        return self.driver.find_element(locator,value)

    def finds(self,locator,value):
        return self.driver.find_elements(locator,value)

    def find_and_click(self,locator,value):
        return self.find(locator,value).click()

    def find_and_send(self,locator,value,contents):
        return self.find(locator,value).send_keys(contents)

    def screenshot(self):
        """截屏保存"""
        pic_name = time.strftime("%Y-%m-%d-%H-%M-%S") + '.PNG'
        screent_path = os.path.join(SCREENSHOTDIR, pic_name)
        self.driver.get_screenshot_as_file(screent_path)
        return screent_path

    def parse(self, yaml_path, fun_name):
        """解析关键字，实现相应动作"""
        with open(yaml_path, "r", encoding="utf-8") as f:
            function = yaml.load(f)
            # 从关键字中取出一个函数
        steps = function.get(fun_name)
        # 解析每一组关键字
        for step in steps:
            # 如果发现关键字是 find_and_click ，就调用已经封装好的 find_and_click 即可
            if step.get("action") == "find_and_click":
                self.find_and_click(step.get('locator'), step.get('value'))
            elif step.get("action") == "find_and_send":
                self.find_and_send(step.get('locator'), step.get('value'), step.get('content'))

    def swipe_find(self, text, num=3):
        """滚动查找元素"""
        for i in range(num):
            if i == num - 1:
                self.driver.implicitly_wait(5)
                raise NoSuchElementException(f"找到{num}次， 未找到。")
            self.driver.implicitly_wait(1)
            try:
                element = self.driver.find_element(MobileBy.XPATH, f"//*[@text='{text}']")
                self.driver.implicitly_wait(5)
                return element
            except:
                print("未找到")
                size = self.driver.get_window_size()
                width = size.get('width')
                height = size.get("height")

                start_x = width / 2
                start_y = height * 0.8

                end_x = start_x
                end_y = height * 0.3

                self.driver.swipe(start_x, start_y, end_x, end_y, 1000)

    def swipe_down(self):
        """滑动屏幕到0.75"""
        size = self.driver.get_window_size()
        width = size.get('width')
        height = size.get('height')
        start_width = width*0.5
        start_height = height*0.25
        end_width = width*0.5
        end_height = height*0.75
        self.driver.swipe(start_width,start_height,end_width,end_height,1000)
